/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.isip.transformation.difference;

import com.isip.transformation.Transformation;

/**
 *
 * @author Fauzan
 */
public class MatrixTransformation implements Transformation{
    
    float[] matrix3x3 = new float[9];;
    
    public MatrixTransformation() {
           
    }
    
    @Override
    public int[][] doTransform(int[][] matrixImage) {
        int[][] new_matrix = new int[matrixImage.length][matrixImage[0].length];
        
        for(int i =1; i < matrixImage.length-1; i++ ) {
            for(int j=1; j < matrixImage[i].length-1; j++) {
                int[] matrix9 = new int[9];
                int idx = 0;
                
                // Up left 0
                if(i > 0 && j > 0) {
                    matrix9[idx] = matrixImage[i-1][j-1];
                }
                
                idx++;
                // Center up 1        
                if(i > 0) {
                     matrix9[idx] = matrixImage[i-1][j];
                }
                
                idx++;
                // Up right 2
                if(i > 0 && j < matrixImage[i].length-1) {
                     matrix9[idx] = matrixImage[i-1][j+1];
                }
                
                idx++;
                // Center Left 3
                if(j > 0) {
                    matrix9[idx] = matrixImage[i][j-1];
                }
                
                idx++;
                // Center 4
                matrix9[idx] = matrixImage[i][j];
                
                idx++;
                // Center Right 5
                 if(j < matrixImage[i].length-1) {
                     matrix9[idx] = matrixImage[i][j+1];
                }
                 
                idx++;
                // Down Left 6
                if(i < matrixImage.length-1 && j > 0) {
                        matrix9[idx] = matrixImage[i+1][j-1];
                }
                
                idx++;
                // Center Down 7
                if(i < matrixImage.length-1) {
                    matrix9[idx] = matrixImage[i+1][j];
                }
                
                idx++;
                // Down Right 8
                if(i < matrixImage.length-1 && j < matrixImage[i].length-1) {
                       matrix9[idx] = matrixImage[i+1][j + 1];
                }
                
                new_matrix[i][j] = dotFunctionMatrix(matrix9);
                
            }
        }
        return new_matrix;
    }
    
    private int dotFunctionMatrix(int[] matrixInput) {
        int sum = 0;
        for(int i=0; i < matrix3x3.length; i++) {
            sum += matrix3x3[i]*matrixInput[i];
        }
        return sum;
    }
    
    public void setMatrixTransformation(float[] matrix) {
        matrix3x3 = matrix;
    }
}
